package com.zhangwei.liwushuo.fragment;


import android.os.Bundle;
import android.widget.ListView;

import com.alibaba.fastjson.TypeReference;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.zhangwei.liwushuo.R;
import com.zhangwei.liwushuo.bean.AppEntity;
import com.zhangwei.liwushuo.bean.Data;
import com.zhangwei.liwushuo.bean.HomeItems;
import com.zhangwei.liwushuo.config.AppConstants;
import com.zhangwei.liwushuo.adapter.vh.HomeItemView;
import com.zw.library.adapter.AAdapter;

import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.FragmentArg;
import org.androidannotations.annotations.res.DimensionRes;

import java.util.ArrayList;

/**
 *
 */
@EFragment(R.layout.common_ptr_layout)
public class HomeItemsFragment extends BasePtrFragment<HomeItems> {
    private ArrayList<HomeItems> homeItemses = new ArrayList<>();
    @FragmentArg(ARG_CHANNELS_INDEX)
    int channelsIndex;

    public static final String ARG_CHANNELS_INDEX = "INDEX";

    @DimensionRes(R.dimen.space_8)
    float dividerHeight;
    private String url;
    TypeReference<AppEntity<Data<HomeItems>>> type = new TypeReference<AppEntity<Data<HomeItems>>>() {
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        bus.register(this);
        url = AppConstants.getHomeItemUrl(channelsIndex);

    }

    @Override
    public void beforeInject() {
        adapter = new AAdapter<>(HomeItemView.class, getActivity());
        adapter.setData(homeItemses);
    }

    @Override
    protected void setAdapter(ListView listView) {
        listView.setDividerHeight((int) dividerHeight);
        listView.setAdapter(adapter);
    }

    @Override
    public void loadData(PullToRefreshListView ptrListView, AAdapter<HomeItems> adapter, int mCurrentPage) {
        helper.get(url, adapter, ptrListView, type, mCurrentPage);
    }

//    @Subscribe
    public void nextUrl(String url) {
        this.url = url;
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        bus.unregister(this);
    }
}
